//
//  API_Pay.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/8.
//

import UIKit
import Moya

enum API_Pay {
    /// 购买章节
    case buyChapter(String, Int, Int)

    /// 创建充值订单
    case creatRechargeOrder(String, String)
    /// 校验订单
    case verifyRecharge(String, String, String)
    
    /// 创建限时充值订单
    case creatLimitedRechargeOrder(String, String, String)
    /// 校验限时订单
    case verifyLimitedRecharge(String, String, String)

    /// 创建阅读充值订单
    case creatReadRechargeOrder(Int, String, String, String)
    /// 校验阅读订单
    case verifyReadRecharge(String, String, String)
    
    /// 创建订阅订单
    case creatSubscribeOrder(String)
    /// 校验订阅订单
    case verifySubscribe(String, String, String)
}

let pay_timeoutClosure = { (endpoint: Endpoint, done: @escaping MoyaProvider<API_Pay>.RequestResultClosure) in
    do {
        var request = try endpoint.urlRequest()
        request.timeoutInterval = 5
        done(.success(request))
    }catch {
        return
    }
}

let provider_pay = MoyaProvider<API_Pay>(requestClosure: pay_timeoutClosure)

extension API_Pay: TargetType {
    var baseURL: URL {
        return URL(string: server.APIHost)!
    }
    
    var path: String {
        switch self {
        case .buyChapter(_, _, _):
            return "/BookStore/order"

        case .creatRechargeOrder(_, _):
            return "/Money/recharge"
        case .verifyRecharge(_, _, _):
            return "/Money/checkRecharge"

        case .creatLimitedRechargeOrder(_, _, _):
            return "/Limitedrecharge/recharge"
        case .verifyLimitedRecharge(_, _, _):
            return "/Limitedrecharge/checkRecharge"

        case .creatReadRechargeOrder(_, _, _, _):
            return "/Chaptertask/recharge"
        case .verifyReadRecharge(_, _, _):
            return "/Chaptertask/checkRecharge"
            
        case .creatSubscribeOrder(_):
            return "/Subscription/saveOrder"
        case .verifySubscribe(_, _, _):
            return "/Subscription/saveSub"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var parameterEncoding: ParameterEncoding {
        return JSONEncoding.default
    }
    
    var sampleData: Data {
        return "test".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        var ps: [String: Any] = ["ver": getAPPVersion(), "channel": "ios", "third_channel": "apple", "timestamp": Date().timeStamp, "userId": getUser()?.userId ?? "", "uuid": getKeyUUId(), "user_channel": getADID()]
        switch self {
        case .buyChapter(let bookId, let chapterId, let gold):
            ps["bookId"] = bookId
            ps["chapterId"] = chapterId == -1 ? "" : chapterId
            ps["gold"] = gold

        case .creatRechargeOrder(let pid, let pname):
            ps["productId"] = pid
            ps["productName"] = pname
            ps["is_sandbox"] = server.isProduct ? 0 : 1
        case .verifyRecharge(let oid, let tid, let receipt):
            ps["orderId"] = oid
            ps["tid"] = tid
            ps["receipt"] = receipt
            ps["is_sandbox"] = server.isProduct ? 0 : 1
        case .creatLimitedRechargeOrder(let pid, let pname, let pids):
            ps["productId"] = pid
            ps["productName"] = pname
            ps["productIds"] = pids
            ps["is_sandbox"] = server.isProduct ? 0 : 1
        case .verifyLimitedRecharge(let oid, let tid, let receipt):
            ps["orderId"] = oid
            ps["tid"] = tid
            ps["receipt"] = receipt
            ps["is_sandbox"] = server.isProduct ? 0 : 1
        case .creatReadRechargeOrder(let taskId, let pid, let pname, let pids):
            ps["taskId"] = taskId
            ps["productId"] = pid
            ps["productName"] = pname
            ps["productIds"] = pids
            ps["is_sandbox"] = server.isProduct ? 0 : 1
        case .verifyReadRecharge(let oid, let tid, let receipt):
            ps["orderId"] = oid
            ps["tid"] = tid
            ps["receipt"] = receipt
            ps["is_sandbox"] = server.isProduct ? 0 : 1
        case .creatSubscribeOrder(let pid):
            ps["productId"] = pid
            ps["is_sandbox"] = server.isProduct ? 0 : 1
        case .verifySubscribe(let oid, let tid, let receipt):
            ps["orderId"] = oid
            ps["tid"] = tid
            ps["receipt"] = receipt
            ps["is_sandbox"] = server.isProduct ? 0 : 1
        }
        return .requestParameters(parameters: ps, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        return ["content-type": "application/json; charset=UTF-8"]
    }
}
